package com.atguigu.guli.service.base.handle;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //最大的异常处理器
    @ExceptionHandler(value = Exception.class)
    public R catchException(Exception e){
        log.error("{}", ExceptionUtils.getStackTrace(e));
        return R.error();
    }

    @ExceptionHandler(value = GuliException.class)
    public R catchException(GuliException e){
        log.error("{}",ExceptionUtils.getStackTrace(e));
        return R.setResult(e.getCodeEnum());
    }
    //为可能出现的异常配备的处理器
    @ExceptionHandler(value = ArithmeticException.class)
    public R catchException(ArithmeticException e){
        log.error("{}",ExceptionUtils.getStackTrace(e));
        return R.setResult(ResultCodeEnum.BY_ZERO_ERROR);
    }


}
